﻿/**
 * 
 */
package views;


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JLabel;

import controllers.ProgressChangeEventListener;

import models.JProgressPanelModel;

/**
 * @author LAP
 *
 */
public class JProgressPanel extends JPanel {
	private JLabel progressEventName;
	
	private JProgressBar eventProgress;
	
	private String caption;
	
	private JProgressPanelModel model;
	/**
	 * 
	 */
	private static final long serialVersionUID = 7893641954758129414L;

	public JProgressPanel(String caption) {
		super();
		this.setCaption(caption);
		createGUI();
	}
	
	private void createGUI() {
		setLayout(new GridBagLayout());
		if (getCaption().equals("")) setCaption("No caption");
		progressEventName = new JLabel(getCaption());
		progressEventName.setHorizontalAlignment(JLabel.LEFT);
		add(progressEventName, new GridBagConstraints(0, 0, 1, 1, 
				1, 0, GridBagConstraints.LINE_START, 
				GridBagConstraints.HORIZONTAL, 
				new Insets(2, 2, 2, 2), 0, 0));
		eventProgress = new JProgressBar(0, 
				100);
		eventProgress.setValue(50);
		add(getEventProgress(), new GridBagConstraints(0, 1, 1, 1,
				1, 1, GridBagConstraints.PAGE_START,
				GridBagConstraints.HORIZONTAL,
				new Insets(0, 2, 2, 2), 0, 0));
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		eventProgress.setStringPainted(true);
	}
	/**
	 * @return the caption
	 */
	public String getCaption() {
		return caption;
	}

	/**
	 * @param caption the caption to set
	 */
	public void setCaption(String caption) {
		this.caption = caption;
	}

	/**
	 * @return the eventProgress
	 */
	public JProgressBar getEventProgress() {
		return eventProgress;
	}

	/**
	 * @return the model
	 */
	public JProgressPanelModel getModel() {
		return model;
	}

	/**
	 * @param model the model to set
	 */
	public void setModel(JProgressPanelModel model) {
		if (model == null) throw new NullPointerException("Model must be not null");
		this.model = model;
	}
	
	public void addProgressChangeEventListner(ProgressChangeEventListener listner) {
		model.addProgressChangeEventListner(listner);
	}
	
	public void removeProgressChangeEventListner(ProgressChangeEventListener listner) {
		model.removeProgressChangeEventListner(listner);
	}
	
	public ProgressChangeEventListener[] getProgressChangeEventListners() {
		return model.getProgressChangeEventListners();
	}
	
	public void setValue(int value) {
		eventProgress.setValue(value);
	}
	
	public void setMaximum(int maximum) {
		eventProgress.setMaximum(maximum);
	}
	
	public void setProgressString() {
		eventProgress.setString(
				Integer.toString(eventProgress.getValue()) + 
				" / " + 
				Integer.toString(eventProgress.getMaximum()));
	}
}
